autowatch=1;
//config vars
margin_top=3;
margin_left=3;
margin_right=8;
grid_size=15;
min_width=150;
min_height=90;

exkludedObjs=["preset-num"];
// lh.bpresize.js

var edges;
var mode = (jsarguments[1]>0);
var obj;

function loadbang() {
    if (!max.loadbangdisabled) {
       // bang();
    }
}

function bang() {
    edges = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY];
    this.patcher.apply(iter);
    //calc_bpatcher('view');
    move = [edges[0]*-1,edges[1]*-1];
    size = [edges[2]-edges[0],edges[3]-edges[1]];
    if (this.patcher.box) {
        this.patcher.box.message("offset",move);
        if(mode){
            var var1=this.patcher.box.varname;
            a=this.patcher.box.getattr("rect");//somehow returns presentation_rect?
            //this.patcher.message("script", "sendbox", "view", "presentation_rect", margin_left ,22,size[0],size[1]);
            this.patcher.parentpatcher.message("script", "sendbox", var1, "presentation_rect", a[0],a[1],size[0],size[1]);
            
            //preset nums reposition 
            var pnum=this.patcher.getnamed('preset-num')
            pnum.message('presentation_position',size[0]-(pnum.rect[2]-pnum.rect[0])-margin_right, margin_top );
        } else{ 
            a = this.patcher.box.rect;
            this.patcher.box.rect = [a[0],a[1],size[0]+a[0],size[1]+a[1]];
        }
    }
}

function resize_child(name) {
    edges = [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY];
    var child=this.patcher.getnamed(name).subpatcher();

    if(child){
        //post('child:'+child+'\n');
       child.apply(iter);
        size = [edges[2]-edges[0],edges[3]-edges[1]];  
        size[0] = Math.max(min_width,size[0]);  
        size[1] = Math.max(min_height,size[1]);  
        size[0]=Math.ceil(size[0]/grid_size)*grid_size;
        size[1]=Math.ceil(size[1]/grid_size)*grid_size;
      
     if(mode){
            this.patcher.message("script", "sendbox", name, "presentation_rect",margin_left ,grid_size,size[0]-margin_right,size[1]);
            //preset nums reposition
            this.patcher.getnamed("header").subpatcher().getnamed("panel").message("presentation_rect",0,0,size[0],grid_size);
            this.patcher.message("script", "sendbox", "header", "presentation_rect",0,4,size[0],grid_size);

            var var1=this.patcher.box.varname;
            a=this.patcher.box.getattr("rect");//somehow returns presentation_rect?
            //this.patcher.message("script", "sendbox", "view", "presentation_rect", margin_left ,22,size[0],size[1]);
            //this.patcher.parentpatcher.message("script", "sendbox", var1, "presentation_rect", a[0],a[1],size[0],size[1]+grid_size);
         
            
        } else{ 
            a = this.patcher.box.rect;
            this.patcher.box.rect = [a[0],a[1],size[0]+a[0],size[1]+a[1]];
        }

            //preset nums reposition 
            var pnum=this.patcher.getnamed('preset-num')
            pnum.message('presentation_position',size[0]-(pnum.rect[2]-pnum.rect[0])-margin_right, margin_top );

    }
}

function iter2(a){
    post(a.getattr('varname'));
}

function iter(a) {
    obj = a;
 //   post(obj.maxclass);
    //post(obj.getattr('varname')+' '+(obj.getattr('varname')==null)+'\n');
  //  if(obj.getattr('varname')=='' || exludedObjs.indexOf(obj.getattr('varname'),0)==-1){
        if (mode) {
            if (a.getattr("presentation")) {
                calc();
            }
        } else {
            calc();
        }
    //}
}

function calc() {
    if (mode) {
        attr = "presentation_rect";
    } else {
        attr = "patching_rect";
    }
    var rect=obj.getattr(attr);
        
    for (i=0; i<2; i++) {
        if(rect[i]<edges[i]) {
            edges[i] = rect[i];
        }
    }
    for (i=2; i<4; i++) {
        if(rect[i]+rect[i-2]>edges[i]) {
            edges[i] = rect[i]+rect[i-2];
        }
    }
}

function calc_bpatcher(name){
        var bp = this.patcher.getnamed(name);
        var a=bp.getattr('rect');
        for (i=0; i<2; i++) {
        if(a[i]<edges[i]) {
           
            edges[i] =a[i];
        }
    }
    for (i=2; i<4; i++) {
        if(a[i]+a[i-2]>edges[i]) {
            edges[i] = a[i]+a[i-2];
        }
    }
}

function setmode(x) {
    mode = (x>0);
}

function resize_parent_box(rect){
    
}



// EOF